package com.example.administrator.jisuan;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;
    private Button button8;
    private Button button9;
    private Button button10;

    private Button buttonjia;
    private Button buttonjian;
    private Button buttoncheng;
    private Button buttonchu;

    private Button buttondian;
    private Button buttonbai;
    private Button buttondel;
    private Button buttontui;
    private Button buttondeng;

    private String firstNumber = "";
    private String secondNumber = "";
    int ch = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      final  TextView textView = (TextView)findViewById(R.id.textView2) ;
        Button button = (Button)findViewById(R.id.button);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "1";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "1";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "2";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "2";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button3 = (Button)findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "3";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "3";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "4";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "4";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button5 = (Button)findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "5";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "5";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button6 = (Button)findViewById(R.id.button6);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "6";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "6";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button7 = (Button)findViewById(R.id.button7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "7";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "7";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button8 = (Button)findViewById(R.id.button8);
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "8";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "8";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button9 = (Button)findViewById(R.id.button9);
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "9";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "9";
                    textView.setText(secondNumber);

                }
            }
        });

        Button button10 = (Button)findViewById(R.id.button10);
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ch == 0) {
                    firstNumber = firstNumber + "0";
                    textView.setText(firstNumber);
                }else{
                    secondNumber = secondNumber + "0";
                    textView.setText(secondNumber);

                }
            }
        });

        Button buttonjia = (Button)findViewById(R.id.button11);
        buttonjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
            ch = 1;
                textView.setText(str + "+");
            }
        });

        Button buttonjian = (Button)findViewById(R.id.button12);
        buttonjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
                ch = 2;
                textView.setText(str + "-");
            }
        });

        Button buttoncheng = (Button)findViewById(R.id.button13);
        buttoncheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
                ch = 3;
                textView.setText( str + "*");
            }
        });

        Button buttondian = (Button)findViewById(R.id.button14);
        buttondian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        Button buttonchu = (Button)findViewById(R.id.button15);
        buttonchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
                ch = 4;
                textView.setText(str + "/");
            }
        });

        Button buttonbai = (Button)findViewById(R.id.button16);
        buttonbai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
                ch = 5;
                textView.setText(str + "%");
            }
        });

        Button buttondel = (Button)findViewById(R.id.button17);
        buttondel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firstNumber = "";
                secondNumber = "";
                ch = 0;
                textView.setText("");
            }
        });

        Button buttontui = (Button)findViewById(R.id.button18);
        buttontui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();

                if(ch==0){
                    if(str.length()==1){
                        textView.setText("");
                        firstNumber ="";
                    }else if(str.length()>1){
                        String  xy = str.substring(0,str.length()-1);
                        firstNumber=firstNumber.substring(0,firstNumber.length()-1);
                        textView.setText(xy);
                    }
                }else {
                    if (str.length() == 1) {
                        textView.setText("");
                        secondNumber = "";
                    } else if(str.length()>1) {
                        String xy = str.substring(0, str.length() - 1);
                        secondNumber = secondNumber.substring(0, secondNumber.length() - 1);
                        textView.setText(xy);
                    }
                }
            }
        });

        Button buttondeng = (Button)findViewById(R.id.button19);
        buttondeng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textView.getText().toString();
                textView.setText(str + "=");
                double first = Double.valueOf(firstNumber);
                double second = Double.valueOf(secondNumber);
                double result = 0;
                switch (ch){
                    case 1:
                        result = first + second;
                        break;
                    case 2:
                        result = first - second;
                        break;
                    case 3:
                        result = first * second;
                        break;
                    case 4:
                        result = first / second;
                        break;
                    default:
                        break;
                }
                textView.setText(result+"");
            }
        });
    }
}
